package com.maize.uwwireless;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class Waterloo extends Activity
{
	private WaterlooWirelessManager manager;
	
	private ImageButton 	loginButton, logoutButton, settingsButton;
	private TextView 		messageBox;
	private ProgressDialog 	progressDialog = null;
	private int 			code;	
	
	private final int SETTINGS = Menu.FIRST;
	private final int HELP = Menu.FIRST + 1;
	private final int ABOUT = Menu.FIRST + 2;
	
	/** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        this.setTitle(R.string.title);
        this.setContentView(R.layout.main);
        
        manager = WaterlooWirelessManager.getInstance(this);
        
        loginButton = (ImageButton)this.findViewById(R.id.login_button);
        loginButton.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v) 
			{
				progressDialog = ProgressDialog.show(Waterloo.this, "Please wait...", "Signing in...", true, true);
			    progressDialog.setIcon(R.drawable.icon);
			    
	    		new Thread( new Runnable()
	    		{
	    			public void run()
	    			{
	    				Looper.prepare();
	    				code = manager.login();
	                    handler.sendEmptyMessage(0);
	                    Looper.loop();
	                    Looper.myLooper().quit();
	    			}
	    		}).start();
			}        	
        });
        
        logoutButton = (ImageButton)this.findViewById(R.id.logout_button);
        logoutButton.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v) 
			{
				progressDialog = ProgressDialog.show(Waterloo.this, "Please wait...", "Signing out...", true, true);
			    progressDialog.setIcon(R.drawable.icon);
			    
	    		new Thread( new Runnable()
	    		{
	    			public void run()
	    			{
	    				Looper.prepare();
	    				code = manager.logout();
	                    handler.sendEmptyMessage(0);
	                    Looper.loop();
	                    Looper.myLooper().quit();
	    			}
	    		}).start();
			}        	
        });
                
        settingsButton = (ImageButton)this.findViewById(R.id.settings_button);
        settingsButton.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v) 
			{
				Intent i = new Intent(Intent.ACTION_MAIN);
        		i.setClassName("com.android.settings", "com.android.settings.wifi.WifiSettings");
            	Waterloo.this.startActivity(i);
			}        	
        });
        
        messageBox = (TextView)this.findViewById(R.id.message);
    }    
	
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if ( event.getKeyCode() == KeyEvent.KEYCODE_BACK )
		{
			this.finish();
		}
		return super.onKeyDown(keyCode, event);		
	}
	
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	super.onCreateOptionsMenu(menu);
    	MenuItem item = menu.add(0, SETTINGS, 0, "Settings");
    	item.setIcon(R.drawable.settings);
    	item = menu.add(0, HELP, 0, "Help");
    	item.setIcon(R.drawable.help);
    	item = menu.add(0, ABOUT, 0, "About");
    	item.setIcon(R.drawable.about);
		return true;    	
    }
    
    public boolean onMenuItemSelected(int featureId, MenuItem item) 
	{
    	switch( item.getItemId() ) 
        {        	       		
        	case SETTINGS:
        		Intent settings = new Intent(this, Settings.class);
            	this.startActivity(settings);
        		return true;
        		
        	case HELP:
        		Intent help = new Intent(this, Help.class);
            	this.startActivity(help);
        		return true;
        		
        	case ABOUT:
        		AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setIcon(R.drawable.icon);
                builder.setTitle("Waterloo for Android");
                builder.setMessage(R.string.about);
                builder.setNeutralButton("OK", null);
                builder.show();
        		return true;
        }
		return true;    	
	}
        
    private Handler handler = new Handler() 
	{
        public void handleMessage(Message msg) 
		{
    		progressDialog.dismiss();
    		
    		String message = "";
    		if (code == WaterlooWirelessManager.SUCCESS)			message = "Success";
    		else if (code == WaterlooWirelessManager.CPEXCEPTION)	message = "ClientProtocolException";
    		else if (code == WaterlooWirelessManager.IOEXCEPTION)	message = "Error: IOException";
    		else if (code == WaterlooWirelessManager.EXCEPTION)		message = "Error";
    		else if (code == WaterlooWirelessManager.ACCEXCEPTION)	message = "Empty username/password";
    		else 													message = "This is crazy";
    		
    		
    		Toast.makeText(Waterloo.this, message, Toast.LENGTH_SHORT).show();
    		
    		messageBox.setText(message);
    		messageBox.setGravity(Gravity.CENTER_HORIZONTAL);
    		
    		if (code == WaterlooWirelessManager.SUCCESS)			messageBox.setTextColor(Color.GREEN);
    		else if (code == WaterlooWirelessManager.CPEXCEPTION) 	messageBox.setTextColor(Color.BLUE);
    		else 													messageBox.setTextColor(Color.RED);
    		
    		message = "";
		}
	};

}
